package cn.com.digitalnet.ad.util;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class EhcacheUtil {
	
	@Autowired protected CacheManager ehcacheManager;
	
	public Object get(String cacheName,Object key){
		Object result = null;
		Cache cache = ehcacheManager.getCache(cacheName);
		if(cache != null){
			Element element = cache.get(key);
			if(element != null){
				result = element.getObjectValue();
			}
		}
		return result;
	}
	
	public void put(String cacheName,Object key,Object value){
		Cache cache = ehcacheManager.getCache(cacheName);
		if(cache != null){
			Element element = new Element(key,value);
			cache.put(element);
		}
	}
}
